package com.edar.web.struts2;

import java.util.List;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public abstract class GenericAction<T> extends SimpleActionSupport implements ModelDriven<T>, Preparable {
	private static final long serialVersionUID = -4958036099343849180L;
	/**
	 * 列表页面中，选择框主键id
	 */
	protected String[] key;
	
	public String[] getKey() {
		return key;
	}

	public void setKey(String[] key) {
		this.key = key;
	}

	/**
	 * 进行CUD操作后,以redirect方式重新打开action默认页的result名.
	 */
	public static final String RELOAD = "reload";

	/**
	 * Action函数,默认action函数，默认指向list函数.
	 */
	@Override
	public String execute() throws Exception {
		return list();
	}

	/**
	 * Action函数,显示Entity列表.
	 * return SUCCESS.
	 */
	public abstract String list() throws Exception;

	/**
	 * Action函数,新增或修改Entity. 
	 * return RELOAD.
	 */
	public abstract String save() throws Exception;

	/**
	 * Action函数,删除Entity.
	 * return RELOAD.
	 */
	public abstract String delete() throws Exception;

	/**
	 * 在save()前执行二次绑定.
	 */
	public void prepareSave() throws Exception {
		prepareModel();
	}
	
	/**
	 * 在input()前执行二次绑定.
	 */
	public void prepareInput() throws Exception {
		prepareModel();
	}

	/**
	 * 屏蔽公共的二次绑定.
	 */
	public void prepare() throws Exception {
	}

	/**
	 * 等同于prepare()的内部函数. 
	 */
	protected abstract void prepareModel() throws Exception;
	
}
